import os
import math

ROOT_PATH = os.path.abspath(os.path.dirname(__file__)) 

def get_all_pcap_path(path):
    file_paths = []
    for dirpath, _, filenames in os.walk(path):
        for filename in filenames:
            file_paths.append(os.path.abspath(os.path.join(dirpath, filename)))
    pcap_paths = list(filter(lambda x: x.endswith('pcap') and " " not in x, file_paths))
    return pcap_paths

def merge_all_pcap_files(file_paths: list):
    cmd = "mergecap -w merged.pcap"
    for file_path in file_paths:
        cmd += ' ' + file_path
    os.system(cmd)

def merge_specific_size_file(file_path, target_size=1):
    origin_file_size = os.path.getsize(file_path)
    times = math.ceil(target_size * 1024 * 1024 / origin_file_size)
    cmd = "mergecap -w {}bytes.pcap".format(target_size) + times * " {}".format(file_path)
    os.system(cmd)


if __name__ == '__main__':
    # merge_all_pcap_files(get_all_pcap_path(ROOT_PATH))
    merge_specific_size_file("./merged.pcap", 5000)